package com.enjoylearning.media.api;

import com.enjoylearning.base.model.PageParams;
import com.enjoylearning.base.model.PageResult;
import com.enjoylearning.media.model.dto.QueryMediaParamsDto;
import com.enjoylearning.media.model.dto.UploadFileParamDto;
import com.enjoylearning.media.model.dto.UploadFileResultDto;
import com.enjoylearning.media.model.po.MediaFiles;
import com.enjoylearning.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @description 媒资文件管理接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
 @Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
 @RestController
public class MediaFilesController {


    @Autowired
    MediaFileService mediaFileService;


    @ApiOperation("媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){
    Long companyId = 1232141425L;
    return mediaFileService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);
    }

    @ApiOperation("上传图片")
    @RequestMapping(value = "/upload/coursefile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadFileResultDto upload(@RequestPart("filedata") MultipartFile multipartFile,
                                      @RequestParam(value= "objectName",required=false) String objectName) throws IOException {
        //文件信息
        UploadFileParamDto uploadFileParamDto = new UploadFileParamDto();
        //原始文件名称
        uploadFileParamDto.setFilename(multipartFile.getOriginalFilename());
        //文件大小
        uploadFileParamDto.setFileSize(multipartFile.getSize());
        //文件类型
        uploadFileParamDto.setFileType("001001");
        //获取文件
        File TempFile = File.createTempFile("md5", ".temp");
        multipartFile.transferTo(TempFile);

        Long companyId = 1L;
        //文件路径
        String absolutePath = TempFile.getAbsolutePath();
        //上传图片
        return mediaFileService.uploadMediaFile(companyId,uploadFileParamDto,absolutePath,objectName);
    }

}
